﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
namespace FTchina.Api
{
    /// <summary>
    ///Web Control 导出到 Excel
    /// </summary>
    public class ToExcel
    {
        public ToExcel()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }


        public static void ControlToExcel(System.Web.UI.Control ctl)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FTchina.basic.GetGuid(5) + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
            // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            ctl.Page.EnableViewState = false;

            ctl.RenderControl(htw);
            string strHtml = sw.ToString().Trim();
            HttpContext.Current.Response.Output.Write(strHtml);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }




        public static void DataSetToExcel(DataSet ds)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FTchina.basic.GetGuid(5) + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
            // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 

            string strHtml = ExportTable(ds);
            HttpContext.Current.Response.Output.Write(strHtml);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
        public static void DataSetToExcel(string sql)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FTchina.basic.GetGuid(5) + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
            // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 

            string strHtml = ExportTable(shujuku.GetDataSet(sql));
            HttpContext.Current.Response.Output.Write(strHtml);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
        private static string ExportTable(DataSet ds)
        {
            string data = "";
            //data = ds.DataSetName + "\n";

            foreach (DataTable tb in ds.Tables)
            {
                //data += tb.TableName + "\n";
                data += "<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">";
                //写出列名
                data += "<tr style=\"font-weight: bold; white-space: nowrap;\">";
                foreach (DataColumn column in tb.Columns)
                {
                    data += "<td>" + column.ColumnName + "</td>";
                }
                data += "</tr>";

                //写出数据
                foreach (DataRow row in tb.Rows)
                {
                    data += "<tr>";
                    foreach (DataColumn column in tb.Columns)
                    {
                        if (column.ColumnName.Equals("ID"))
                            data += "<td style=\"vnd.ms-excel.numberformat:@\">" + row[column].ToString() + "</td>";
                        else
                            data += "<td>" + row[column].ToString() + "</td>";
                    }
                    data += "</tr>";
                }
                data += "</table>";
            }

            return data;
        }

        #region html table 导出到Excel
        public static void HtmlTableToExcel(string table)
        {
            table = table.Replace("<table", "<table x:str");//将单元格设置为文本

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".xls");
            // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 

            string head = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>" +
                "<meta http-equiv=Content-Type content='text/html; charset=UTF-8'>";
            table = head + table;
            HttpContext.Current.Response.Output.Write(table);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }

        #endregion


    }
}